package com.manytory.torycoupon;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import com.manytory.torycoupon.data.ToryCoupon_App;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;

public class LoginTypeSelectedActivity extends Activity implements OnClickListener{
	Button btnEmail, btnSMS;
	ToryCoupon_App tory_app;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_type_selected);
		
		tory_app = (ToryCoupon_App)getApplication();
		
		btnEmail = (Button)findViewById(R.id.btn_EmailLogin);
		btnEmail.setOnClickListener(this);
		
		btnSMS = (Button)findViewById(R.id.btn_SMSCert);
		btnSMS.setOnClickListener(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		
		if(ToryCoupon_App.strDeviceSerial == "")
		{
			String sANDROID_ID = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID); 
			tory_app.saveDeviceSerialPreferences(sANDROID_ID);
			Log.d("min_test", "save android_id :  " + sANDROID_ID);
		}
		else
		{
			Log.d("min_test", "load android_id :  " + ToryCoupon_App.strDeviceSerial);
		}
		
		super.onStart();
	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
	}

	@Override
	public void onClick(View v) {
		switch(v.getId())
		{
		case R.id.btn_EmailLogin:
			//startActivity(new Intent(LoginTypeSelectedActivity.this, EmailLoginActivity.class));
		{
			try {
				HttpClient client = new DefaultHttpClient();
				String postURL = getString(R.string.tory_URL) + getString(R.string.tory_URL_sms_cert_send);
				HttpPost post = new HttpPost(postURL);
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair("serialNum", "0000"));
				params.add(new BasicNameValuePair("phoneNum", "1111"));
				params.add(new BasicNameValuePair("pushKey", "2222"));
				params.add(new BasicNameValuePair("passFlag", "3333"));
				UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params, HTTP.UTF_8);
				post.setEntity(ent);
				HttpResponse responsePost = client.execute(post);
				HttpEntity resEntity = responsePost.getEntity();
				
				if(resEntity != null) {
					Log.d("min_test", "xmlValues : " + EntityUtils.toString(resEntity));
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
			break;
		case R.id.btn_SMSCert:
			startActivity(new Intent(LoginTypeSelectedActivity.this, PhoneNumActivity.class));
			break;
		default:
			break;
		}
	}

}
